<template>
    <div class="businessSelectConf_option">
        <div class="businessSelectConf_content">
            <Form ref="optionForm" :model="optionForm" :rules="optionFormValidate" :label-width="140">
                <div class="businessSelectConf">
                    <div class="common_br">
                        <div class="common_content">
                            <Card>
                                <h2 class="common_title" style="font-size: 15px;">业务选择配置</h2>
                                <Row style="margin-left: 32%;">
                                    <Col span="10">
                                        <FormItem label="业务名称:" prop="businessName">
                                            <Input v-model="optionForm.businessName" maxlength="32"
                                                   style="width: 250px;"
                                                   placeholder="请输入业务名称"/>
                                        </FormItem>
                                        <FormItem label="业务描述:" prop="businessDesc">
                                            <Input v-model="optionForm.businessDesc" maxlength="600" show-word-limit
                                                   type="textarea"
                                                   placeholder="请输入业务描述..." style="width: 350px;"/>
                                        </FormItem>
                                        <FormItem label="业务类型:" style="width: 600px" prop="businessType">
                                            <CheckboxGroup v-model="optionForm.businessType">
                                                <Checkbox label="01">
                                                    <span>新车</span>
                                                </Checkbox>
                                                <Checkbox label="02">
                                                    <span>二手车</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                        <FormItem label="车辆类型:" style="width: 600px" prop="carType">
                                            <CheckboxGroup v-model="optionForm.carType">
                                                <Checkbox label="00">
                                                    <span>乘用车</span>
                                                </Checkbox>
                                                <Checkbox label="01">
                                                    <span>LCV</span>
                                                </Checkbox>
                                                <Checkbox label="02">
                                                    <span>中卡</span>
                                                </Checkbox>
                                                <Checkbox label="03">
                                                    <span>重卡</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                        <FormItem label="车辆属性:" style="width: 600px" prop="carNature">
                                            <CheckboxGroup v-model="optionForm.carNature">
                                                <Checkbox label="01">
                                                    <span>新车</span>
                                                </Checkbox>
                                                <Checkbox label="02">
                                                    <span>挂牌车</span>
                                                </Checkbox>
                                                <Checkbox label="03">
                                                    <span>二手车</span>
                                                </Checkbox>
                                                <Checkbox label="04">
                                                    <span>认证二手车</span>
                                                </Checkbox>
                                                <Checkbox label="05">
                                                    <span>试乘试驾车</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                        <FormItem label="运营方式:" style="width: 600px" prop="operateWay">
                                            <CheckboxGroup v-model="optionForm.operateWay">
                                                <Checkbox label="00">
                                                    <span>无</span>
                                                </Checkbox>
                                                <Checkbox label="01">
                                                    <span>网约车</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                        <FormItem label="挂靠方式:" style="width: 600px" prop="affiliatedWay">
                                            <CheckboxGroup v-model="optionForm.affiliatedWay">
                                                <Checkbox label="00">
                                                    <span>无</span>
                                                </Checkbox>
                                                <Checkbox label="01">
                                                    <span>第三方公司挂靠</span>
                                                </Checkbox>
                                                <Checkbox label="02">
                                                    <span>个体工商挂靠</span>
                                                </Checkbox>
                                                <Checkbox label="03">
                                                    <span>网约车挂靠</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                        <FormItem label="购车目的:" style="width: 600px" prop="carPurpose">
                                            <CheckboxGroup v-model="optionForm.carPurpose">
                                                <Checkbox label="00">
                                                    <span>自用</span>
                                                </Checkbox>
                                                <Checkbox label="01">
                                                    <span>商用</span>
                                                </Checkbox>
                                            </CheckboxGroup>
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row style="margin-left: 40%;">
                                    <Button type="info" icon="ios-bookmark" @click.native="jumpToList(optionForm)">
                                        保存
                                    </Button>
                                    <Button type="info" icon="ios-backspace-outline" @click="jump()">返回</Button>
                                    <Button type="primary" icon="ios-backspace-outline" @click="ruleAdd()">规则配置</Button>
                                </Row>
                                <Modal v-model="showRuleConfig"
                                       :title="ruleTitle"
                                       :width="1100"
                                       :mask-closable="false"
                                >
                                    <Row>
                                        <Button type="info" icon="ios-egg" :disabled="isActive" @click="invalid">
                                            {{activeName}}
                                        </Button>
                                        <Button type="error" icon="ios-egg-outline" :disabled="isDeActive"
                                                @click="deActiveRule">{{deActiveName}}
                                        </Button>
                                    </Row>
                                    <rule-tree v-if="dataLoad" @saved="ruleSaved"
                                               :rule-data="ruleData"
                                               :ruleAtoms="ruleAtoms" :rule-id="ruleId" :ruleName="ruleName"
                                               :ruleGroup="ruleGroup" :businessScenario="businessScenario"/>
                                </Modal>
                            </Card>
                        </div>
                    </div>
                </div>
            </Form>
        </div>
    </div>
</template>
<script>
    import ruiRules from '_c/rui-auto/rui-rules/index';
    import {
        getInfo,
        getRuleInfoByRuleNo,
        saveBusinessConfInfo,
        updateBusinessConfInfo
    } from "../../../api/apply/business-scenarios/businessScenariosApi";
    import {loadRuleDataById} from '_p/basic/api/rule/rule';
    import {queryAtomsByBizType} from '_p/basic/api/rule/ruleAtom'
    import {updateRuleState} from "../../../api/afs-case/parameter-rules/topRules";

    export default {
        name: 'businessSelectConf',
        components: {ruleTree: ruiRules},
        data() {
            const validateMultiple = (rule, value, callback) => {
                if (value.length < 1) {
                    return callback(new Error('不少于一项'));
                } else {
                    callback();
                }
            };
            const validateMultiple2 = (rule, value, callback) => {
                if (value.length > 1) {
                    return callback(new Error('不可多选'));
                } else {
                    callback();
                }
            };
            //选择新车：车辆属性只能选择新车、挂牌车
            // 选择二手车：车辆属性只选择试乘试驾车、认证二手车、二手车
            const validateMultiple3 = (rule, value, callback) => {
                console.log(this.optionForm.businessType);
                if (this.optionForm.businessType == '01') {
                    if (value != '01' && value != '02') {
                        return callback(new Error('只可选择新车或挂牌车'));
                    } else {
                        callback();
                    }
                }
                if (value != '03' && value != '04' && value != '05') {
                    return callback(new Error('只可选择试乘试驾车、认证二手车或二手车'));
                } else {
                    callback();
                }
            };
            return {
                showRuleConfig: false,
                ruleTitle: "规则配置",
                isActive: false,
                isDeActive: false,
                activeName: "激活",
                deActiveName: "失效",
                isAdd: false,
                //规则变量
                ruleAtoms: [],
                ruleName: '规则配置',
                ruleGroup: 'businessTypeRule',
                businessScenario: '',
                ruleData: [],
                ruleId: '',
                bizType: 'businessTypeRule',
                dataLoad: false,
                optionForm: {
                    id: '',
                    atomId: "",
                    businessName: '',
                    businessDesc: '',
                    businessType: [],
                    carType: [],
                    carNature: [],
                    operateWay: [],
                    affiliatedWay: [],
                    carPurpose: [],
                },
                //校验
                optionFormValidate: {
                    businessName: [
                        {required: true, message: "业务名称不能为空", trigger: "blur"},
                        {type: 'string', message: "业务名称不能为空", trigger: "blur"}
                    ],
                    businessDesc: [
                        {required: true, message: "业务描述不能为空", trigger: "blur"},
                        {type: 'string', message: "业务描述不能为空", trigger: "blur"}
                    ],
                    carType: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",}
                    ],
                    carNature: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",},
                        {validator: validateMultiple3, trigger: "change"},
                    ],
                    operateWay: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",}
                    ],
                    affiliatedWay: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",}
                    ],
                    carPurpose: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",}
                    ],
                    businessType: [
                        {validator: validateMultiple, trigger: "change",},
                        {validator: validateMultiple2, trigger: "change",}
                    ]
                },
            }
        },
        mounted() {
            this.initGetParam();
        },
        methods: {
            //打开规则配置窗口
            ruleAdd() {
                this.showRuleConfig = true;
            },
            //激活规则
            invalid(row) {
                console.log(row, "规则信息")
                updateRuleState(row).then(res => {
                    if (res.code === "0000") {
                        this.isDeActive = false;
                        this.isActive = true;
                        this.activeName = "已激活";
                        this.deActiveName = "失效";
                        this.$Message.success("操作成功");
                        this.getList();
                    }
                })
            },
            //失效规则
            deActiveRule() {
                this.isActive = false;
                this.isDeActive = true;
                this.deActiveName = "已失效";
                this.activeName = "激活";
            },

            ruleSaved(ruleId) {
                this.ruleId = ruleId;
                console.log("规则id:", this.ruleId)
                let param = {
                    id: this.optionForm.id,
                    atomId: this.ruleId,
                    businessScenario: this.optionForm.id,
                    businessName: this.optionForm.businessName,
                    businessDesc: this.optionForm.businessDesc,
                    businessTypeTemp: this.optionForm.businessType,
                    carTypeTemp: this.optionForm.carType,
                    carNatureTemp: this.optionForm.carNature,
                    operateWayTemp: this.optionForm.operateWay,
                    affiliatedWayTemp: this.optionForm.affiliatedWay,
                    carPurposeTemp: this.optionForm.carPurpose,
                };
                console.log("数据:", param)
                updateBusinessConfInfo(param).then(res => {
                    if (res.code === "0000") {
                        //跳转到新页面
                        this.$Message.success("规则id已存入");
                    }
                });
            },
            initGetParam() {
                if (this.afs.getPageParams(this) != null && this.afs.getPageParams(this) !== "") {
                    this.optionForm.id = this.afs.getPageParams(this).id;
                    this.isAdd = this.afs.getPageParams(this).isAdd;
                    this.businessScenario = this.optionForm.id;
                    if (this.isAdd === false) {
                        this.getInfoById();
                        this.getRuleInfoByRuleNo();
                    }
                }
            },
            getInfoById() {
                let param = {
                    id: this.optionForm.id
                }
                getInfo(param).then(res => {
                    if (res.code === "0000") {
                        console.log("this.optionForm.id", this.optionForm.id)
                        this.optionForm = res.data;
                        this.optionForm.atomId = res.data.atomId;
                        this.optionForm.businessName = res.data.businessName;
                        this.optionForm.businessDesc = res.data.businessDesc;
                        this.optionForm.businessType = [res.data.businessType];
                        this.optionForm.carType = [res.data.carType];
                        this.optionForm.carNature = [res.data.carNature];
                        this.optionForm.operateWay = [res.data.operateWay];
                        this.optionForm.affiliatedWay = [res.data.affiliatedWay];
                        this.optionForm.carPurpose = [res.data.carPurpose];
                        // this.isShow = true;
                    }
                    ;
                });
            },
            //通过业务选择配置id查询规则id
            getRuleInfoByRuleNo() {
                let params = {
                    ruleNo: this.optionForm.id,
                }
                //查询规则
                getRuleInfoByRuleNo(params).then(res => {
                    //规则返回规则id，显示规则管理
                    if (res.code === '0000') {
                        console.log("编辑跳转，获取ID:", res.data)
                        if (typeof (res.data) == "undefined") {
                            this.ruleId = "";
                        } else {
                            this.ruleId = res.data.id;
                            console.log("编辑跳转，获取ID:", res.data.id)
                        }
                        loadRuleDataById((this.ruleId && this.ruleId != '') ? this.ruleId : undefined).then(res => {
                            if (res.code === '0000') {
                                if (res.data.length > 0) {
                                    this.ruleData.push(...res.data);
                                }
                                this.dataLoad = true;
                            }
                        });
                        queryAtomsByBizType(this.bizType, "case").then(res => {
                            if (res.code === "0000") {
                                this.ruleAtoms = res.data;
                            }
                        })
                    }
                });
            },

            jumpToList() {
                this.$refs.optionForm.validate(async (valid) => {
                    if (valid) {
                        let param = {
                            id: this.optionForm.id,
                            atomId: this.ruleId,
                            businessScenario: this.optionForm.id,
                            businessName: this.optionForm.businessName,
                            businessDesc: this.optionForm.businessDesc,
                            businessTypeTemp: this.optionForm.businessType,
                            carTypeTemp: this.optionForm.carType,
                            carNatureTemp: this.optionForm.carNature,
                            operateWayTemp: this.optionForm.operateWay,
                            affiliatedWayTemp: this.optionForm.affiliatedWay,
                            carPurposeTemp: this.optionForm.carPurpose,
                        };
                        if (this.isAdd) {
                            //保存
                            saveBusinessConfInfo(param).then(res => {
                                if (res.code === "0000") {
                                    this.$Message.success("操作成功");
                                    this.afs.closeTab(this);
                                }
                                ;
                            });
                        } else if (this.isAdd === false) {
                            //更新
                            updateBusinessConfInfo(param).then(res => {
                                if (res.code === "0000") {
                                    //跳转到新页面
                                    this.$Message.success("操作成功");
                                    this.afs.closeTab(this);
                                }
                            });
                        }
                    }
                });
            }
            ,
            jump() {
                this.afs.closeTab(this);
            },
        }
    }
</script>
